package com.rbac.admin.controller.system;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.rbac.admin.annotation.OperationLog;
import com.rbac.admin.common.result.Result;
import com.rbac.admin.dto.system.OperationLogQueryDTO;
import com.rbac.admin.entity.system.SysOperationLog;
import com.rbac.admin.service.system.SysOperationLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 系统操作日志Controller
 * 
 * @author RBAC-Admin
 * @since 2024-01-01
 */
@Slf4j
@RestController
@RequestMapping("/system/operation-log")
public class SysOperationLogController {

    @Autowired
    private SysOperationLogService operationLogService;

    /**
     * 分页查询操作日志
     */
    @PostMapping("/page")
    @OperationLog(module = "系统监控", type = "查询", value = "查询操作日志")
    public Result<IPage<SysOperationLog>> getLogPage(@RequestBody OperationLogQueryDTO queryDTO) {
        
        IPage<SysOperationLog> page = operationLogService.selectLogPage(
                queryDTO.getCurrent(),
                queryDTO.getSize(),
                queryDTO.getUsername(),
                queryDTO.getRoleName(),
                queryDTO.getOperationModule(),
                queryDTO.getOperationType(),
                queryDTO.getOperationResult(),
                queryDTO.getStartTime(),
                queryDTO.getEndTime()
        );
        
        return Result.success(page);
    }

    /**
     * 查看日志详情
     */
    @GetMapping("/detail/{id}")
    @OperationLog(module = "系统监控", type = "查询", value = "查看操作日志详情")
    public Result<SysOperationLog> getLogDetail(@PathVariable Long id) {
        SysOperationLog log = operationLogService.getById(id);
        return Result.success(log);
    }

    /**
     * 清理历史日志
     */
    @PostMapping("/clean")
    @OperationLog(module = "系统监控", type = "删除", value = "清理历史操作日志")
    public Result<Integer> cleanOldLogs(@RequestParam(defaultValue = "30") int days) {
        int count = operationLogService.cleanOldLogs(days);
        return Result.success("成功清理" + count + "条历史日志", count);
    }
}